<?php
/**
 * Visit is an instance of CActiveRecord (sytem.db.ar).
 *
 * CActiveRecord is the base class for classes representing relational data.
 *
 * Eduow :  International system for education (http://www.eduow.com)
 * Copyright 2010, Nikolay Stoitsev
 *
 * Licensed under GNU General Public License version 2
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright 2010
 * @link          http://eduow.com Eduow Project
 * @package       eduow
 * @subpackage    application.models
 * @inheritance   class Visit >> CActiveRecord >> CModel >> CComponent
 * @implements    ArrayAccess, Traversable, IteratorAggregate
 * @since         Eduow v 0.0.4
 * @license       http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License version 2
 */

class Visit extends CActiveRecord
{
    /**
     * The followings are the available columns in table 'visits':
     * @var integer $id
     * @var string $ip_adr
     * @var string $referer
     * @var string $country
     * @var string $client
     * @var string $visit_date
     * @var string $time
     * @var string $on_page
     */

    public $referer;
    public $delay = 1;

    /**
     * Returns the static model of the specified AR class.
     * @return CActiveRecord the static model class
     */
    public static function model($className=__CLASS__){
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName(){
        return 'visits';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules(){
        return array(
            array('ip_adr', 'length', 'max'=>15),
            array('referer', 'length', 'max'=>250),
            array('country', 'length', 'max'=>2),
            array('client', 'length', 'max'=>500),
            array('visit_date, time', 'safe'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels(){
        return array(
            'id' => Yii::t('yii','Id'),
            'ip_adr' => Yii::t('yii','Ip Adrress'),
            'referer' => Yii::t('yii','Referer'),
            'country' => Yii::t('yii','Country'),
            'client' => Yii::t('yii','Client'),
            'visit_date' => Yii::t('yii','Visit Date'),
            'time' => Yii::t('yii','Time'),
            'on_page' => Yii::t('yii','On Page'),
        );
    }

    function get_country() {
        $db = app()->db;
        $db->setActive(true);
        $cnt=$_SERVER['REMOTE_ADDR'];
        $country_sql = "SELECT country FROM ip2nation WHERE ip < INET_ATON('{$cnt}') ORDER BY ip DESC LIMIT 0,1";

        $result = $db->createCommand($country_sql)->query();
        $res = $result->read();
        return $res['country'];
    }

    function check_last_visit() {
        $model = new Visit;
        $models = Visit::find(array('select'=>'time','condition'=>"visit_date = CURDATE() AND ip_adr = '{$_SERVER['REMOTE_ADDR']}'"));

        if ($models != 0) {
            $last_hour = date("H") - $this->delay;
            $check_time = date($last_hour."is");
            if ($models < $check_time) {
                return true;
            } else {
                return false;
            }
        } else {
            return true;
        }
    }

}
